package com.example.zingalbumdownloader.utility;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.util.Log;

public class StringUtils {

	static ArrayList<String> pids;

	@SuppressLint("NewApi")
	public static String unAccent(String s) {
		String temp = Normalizer.normalize(s, Normalizer.Form.NFD);
		Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
		// return pattern.matcher(temp).replaceAll("");
		return pattern.matcher(temp).replaceAll("").replaceAll("Đ", "D")
				.replace("đ", "d");
	}

	public static String getStringFromURLString(String urlString)
			throws IOException {
		BufferedReader br = null;
		String result = "";
		// try {
		URL url = new URL(urlString);
		StringBuilder sb = new StringBuilder();
		br = new BufferedReader(new InputStreamReader(url.openStream()));
		String line;
		while ((line = br.readLine()) != null) {
			sb.append(line + "\n");
		}
		result = sb.toString();
		// } catch (IOException ex) {
		// Log.e("ZING", "StringFromURL1 " + ex.toString());
		// } finally {
		// try {
		br.close();
		// } catch (IOException ex) {
		// Log.e("ZING", "StringFromURL2 " + ex.toString());
		// }
		// }
		return result;
	}

	public static String getPlaylistString(String input) {
		int start = 0, end = 0;
		start = input.indexOf("content-block") - 12;
		end = input.indexOf("paging") - 12;
		String result = input.substring(start, end);
		return result;
	}

	public static ArrayList<String> findPidsFromString(String stringHTML) {
		pids = new ArrayList<String>();
		int index = 0;
		while ((index = stringHTML.indexOf("pid=", index)) != -1) {
			if (!stringHTML.substring(index + 5, index + 13).equals(""))
				pids.add(stringHTML.substring(index + 5, index + 13));
			index += 13;
		}
		// Log.d("ZING", "Getpids");
		return pids;
	}

	public static String generateSearchLink(String keyword) {
		StringBuffer sb = new StringBuffer();
		sb.append(MyResource.timkiem_playlist);
		keyword = keyword.trim();
		int index = 0;
		int next;
		while ((next = keyword.indexOf(" ", index)) != -1) {
			String temp;
			sb.append((temp = keyword.substring(index, next)) + "+");
			keyword = keyword.replace(temp, "");
			keyword = keyword.trim();
		}
		sb.append(keyword);
		return sb.toString();
	}

	public static String findAlbum_XML(String stringIn) {
		int startIndex = stringIn.indexOf("xml") + 5;
		int endIndex = stringIn.indexOf("class", startIndex) - 2;
		Log.d("ZING", "xml " + stringIn.substring(startIndex, endIndex));
		return stringIn.substring(startIndex, endIndex);
	}

	public static ArrayList<Integer> getAllIndexSubString(String input,
			String find, int delay) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		int index = input.indexOf(find);
		// int count = 0;
		while (index >= 0) {
			list.add(index + delay);
			// count++;
			index = input.indexOf(find, index + 1);
		}
		// System.out.println(count);
		return list;
	}

	public static String insertAllToString(String input, String insert,
			List<Integer> allindex) {
		StringBuilder builder = new StringBuilder(input);
		int add = 0;
		for (Integer index : allindex) {
			builder.insert(index + add, insert);
			add++;
		}
		return builder.toString();
	}
}
